package com.grq.mis.component;

import cn.hutool.core.util.IdUtil;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Slf4j
@Component
public class UploadComponent {

    @Autowired
    private MinioClient minioClient;
    @Value("${minio.url}")
    private String url;
    @Value("${minio.bucket}")
    private String bucket;

    public String uploadImg(MultipartFile file){
        // 文件拓展名
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
        String uuid = IdUtil.simpleUUID();
        String imgName = uuid + "." + suffix;

        PutObjectOptions options = new PutObjectOptions(file.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
        options.setContentType(file.getContentType());

        try {
            minioClient.putObject(bucket, imgName, file.getInputStream(), options);
        } catch (ErrorResponseException e) {
            e.printStackTrace();
            throw new BusinessException(ResultEnum.UPLOAD_FILE_ERROR, ResultEnum.UPLOAD_FILE_ERROR.getMsg() + "=>" + e.getMessage());
        } catch (InsufficientDataException e) {
            e.printStackTrace();
            throw new BusinessException(ResultEnum.UPLOAD_FILE_ERROR, ResultEnum.UPLOAD_FILE_ERROR.getMsg() + "=>" + e.getMessage());
        } catch (InternalException e) {
            e.printStackTrace();
            throw new BusinessException(ResultEnum.UPLOAD_FILE_ERROR, ResultEnum.UPLOAD_FILE_ERROR.getMsg() + "=>" + e.getMessage());
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
            throw new BusinessException(ResultEnum.UPLOAD_FILE_ERROR, ResultEnum.UPLOAD_FILE_ERROR.getMsg() + "=>" + e.getMessage());
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            throw new BusinessException(ResultEnum.UPLOAD_FILE_ERROR, ResultEnum.UPLOAD_FILE_ERROR.getMsg() + "=>" + e.getMessage());
        } catch (InvalidResponseException e) {
            e.printStackTrace();
            throw new BusinessException(ResultEnum.UPLOAD_FILE_ERROR, ResultEnum.UPLOAD_FILE_ERROR.getMsg() + "=>" + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(ResultEnum.UPLOAD_FILE_ERROR, ResultEnum.UPLOAD_FILE_ERROR.getMsg() + "=>" + e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new BusinessException(ResultEnum.UPLOAD_FILE_ERROR, ResultEnum.UPLOAD_FILE_ERROR.getMsg() + "=>" + e.getMessage());
        } catch (XmlParserException e) {
            e.printStackTrace();
            throw new BusinessException(ResultEnum.UPLOAD_FILE_ERROR, ResultEnum.UPLOAD_FILE_ERROR.getMsg() + "=>" + e.getMessage());
        }

        String imgUrl = url + "/" + bucket + "/" + imgName;
        log.info("上传图片地址：" + imgUrl);

        return imgUrl;
    }

}
